package masterjava.client.gxt.highstock;

import com.extjs.gxt.ui.client.widget.Component;
import com.google.gwt.user.client.Random;
import masterjava.client.GxtTestEntryPoint;
import masterjava.common.client.gxt.highstock.HighStockChart;
import masterjava.common.client.gxt.highstock.HighStockTabs;
import masterjava.common.client.gxt.highstock.HighStockType;
import masterjava.common.client.gxt.highstock.intraday.IntradayHighStockSeries;
import masterjava.common.client.gxt.highstock.intraday.IntradayVolumeHighStockSeries;
import masterjava.common.shared.charts.IntradaysEntry;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: gkislin
 * Date: 7/20/11
 */
public class HighStockEntryPoint extends GxtTestEntryPoint {
    private static final int NUMBER = 1000;

    private class TestEntry implements IntradaysEntry {
        private final Date timestamp;
        private final Double value;
        private final Double volume;

        private TestEntry(Date ts, Double value, Double volume) {
            this.timestamp = ts;
            this.value = value;
            this.volume = volume;
        }

        @Override
        public Date getTimestamp() {
            return timestamp;
        }

        @Override
        public Double getValue() {
            return value;
        }

        @Override
        public Double getVolume() {
            return volume;
        }
    }

    @Override
    protected Component getContent() {
        List<TestEntry> list = new ArrayList<TestEntry>(NUMBER);
        long now = new Date().getTime();
        Double plBase = 100.;
        int shift = 50000000 / NUMBER;
        for (int i = 0; i < NUMBER; i++) {
            now += Random.nextInt(shift);
            plBase += (Random.nextDouble() - .2) * 3.;
            list.add(new TestEntry(new Date(now), plBase, Random.nextDouble() * 1000.));
        }

        HighStockTabs tabs = new HighStockTabs<HighStockChart>(HighStockType.getTypesMap().values()) {

            @Override
            protected HighStockChart createTabComponent(HighStockType item) {
                return createChart(item);
            }
        };
        tabs.setSeries(
                new IntradayHighStockSeries<TestEntry>(list),
                new IntradayVolumeHighStockSeries<TestEntry>(list));
        return tabs;
    }
}
